﻿using Core.Enums;
using Core.Formats;
using Core.ReadWriter;
using Core.Tools;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;

namespace Test
{
    public class Test_Json
    {
        public unsafe static void Test()
        {
            //JsonFormat format = new JsonFormat();
            //Dictionary<string, object> Object = new Dictionary<string, object>();

            //Object.Add("Code", 0);
            //Object.Add("Msg", "\"wo\"\n\"ai\"\n\"ni\"");

            //Console.WriteLine(format.Encode((object)Object));
            //var testFiles = new string[] {
            //    @"E:\NewApp\TestFile\.cache.json",
            //    @"E:\NewApp\TestFile\ListAll.json",
            //    @"E:\NewApp\TestFile\catalog.json",
            //    @"E:\NewApp\TestFile\_package.json",
            //    @"E:\NewApp\TestFile\47250.json",
            //    @"E:\NewApp\TestFile\components.json",
            //};


            //foreach (var Item in testFiles)
            //{
            //    var FileItem = new FileInfo(Item);
            //    var Text = File.ReadAllText(FileItem.FullName);
            //    var Object = new JsonFormat().Decode<Dictionary<string, object>>(Text);

            //    TestEncode(FileItem.Name, Object, 100);
            //    TestDecode(FileItem.Name, Text, 100);
            //}

            var b = BaseReadWriter.Create(typeof(Child));

            Console.WriteLine(b["Name"]);

            Console.WriteLine(new JsonFormat().Encode(b));
        }

        public class Base
        {
            public string Name { get; set; } = "Old";
        }

        public class Child : Base
        {
            public new string Name { get; set; } = "New";
        }

        public static void TestDecode(string TestName, string Text, int Count)
        {
            var format = new JsonFormat();

            Console.WriteLine("Test decode : " + TestName);
            var Begin = DateTime.Now;

            for (int i = 0; i < Count; i++)
            {
                LitJson.JsonMapper.ToObject(Text);
            }

            Console.WriteLine("\t LitJson use time : " + (DateTime.Now - Begin).TotalMilliseconds);

            Begin = DateTime.Now;

            for (int i = 0; i < Count; i++)
            {
                format.Decode<Dictionary<string, object>>(Text);
            }

            Console.WriteLine("\t JsonFormat use time : " + (DateTime.Now - Begin).TotalMilliseconds);

            Begin = DateTime.Now;

            for (int i = 0; i < Count; i++)
            {
                Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(Text);
            }

            Console.WriteLine("\t Newtonsoft.Json use time : " + (DateTime.Now - Begin).TotalMilliseconds);
        }

        public static void TestEncode(string TestName, object Object, int Count)
        {
            var format = new JsonFormat();

            Console.WriteLine("Test encode : " + TestName);
            var Begin = DateTime.Now;

            for (int i = 0; i < Count; i++)
            {
                LitJson.JsonMapper.ToJson(Object);
            }

            Console.WriteLine("\t LitJson use time : " + (DateTime.Now - Begin).TotalMilliseconds);

            Begin = DateTime.Now;

            for (int i = 0; i < Count; i++)
            {
                format.Encode(Object);
            }

            Console.WriteLine("\t JsonFormat use time : " + (DateTime.Now - Begin).TotalMilliseconds);

            Begin = DateTime.Now;

            for (int i = 0; i < Count; i++)
            {
                Newtonsoft.Json.JsonConvert.SerializeObject(Object);
            }

            Console.WriteLine("\t Newtonsoft.Json use time : " + (DateTime.Now - Begin).TotalMilliseconds);

            
        }
    }
}